package com.yxjme.stateviewlayoutlibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;


public class StateViewLayout extends FrameLayout {

    /**什么状态都不是*/
    public static final int NONE = 0x0;
    /**加载状态*/
    public static final int LOADING = 0x1;
    /**无数据或者加载失败状态*/
    public static final int NO_DATA = 0x2;
    /**无网络状态*/
    public static final int NO_NET = 0x3;
    /**默认当前状态*/
    public int CURRENT_STATE = NONE;
    /**无数据时icon*/
    private int noDataIcon;
    /**无网络时icon*/
    private int noNetIcon;
    /**无数据时文字描述*/
    private String noDataText;
    /**无网络时文字描述*/
    private String noNetText="网络连接不可用！";
    /**文字颜色*/
    private int showTextColor ;
    private int NoNetLayoutResID ;
    private int NoDataLayoutResID ;
    private int LoadingLayoutResID ;

    private CommonStateView noDataCommonStateView;
    private CommonStateView noNetCommonStateView ;
    private LoadingView loadingView;

    /**
     * 加载圈的大小
     */
    private float loadViewSize ;


    public void setNoDataIcon(int noDataIcon) {
        this.noDataIcon = noDataIcon;
    }

    public void setNoDataText(String noDataText) {
        this.noDataText = noDataText;
    }

    public void setNoNetIcon(int noNetIcon) {
        this.noNetIcon = noNetIcon;
    }

    public void setNoNetText(String noNetText) {
        this.noNetText = noNetText;
    }

    public StateViewLayout(Context context) {
        this(context,null);
    }

    public StateViewLayout(Context context,AttributeSet attrs) {
        this(context, attrs,0);
    }

    public StateViewLayout(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(context,attrs,defStyleAttr);
    }


    /**
     * 初始化屬性
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    private void initAttr(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.StateViewLayout, 0, defStyleAttr);
        int count = array.getIndexCount();
        for (int i = 0 ; i<count ;i++){
            int attr = array.getIndex(i);
            if (attr == R.styleable.StateViewLayout_noDataIcon) {
                noDataIcon = array.getResourceId(attr,0);
            }
            else if (attr == R.styleable.StateViewLayout_noDataText){
                noDataText = array.getString(attr);
            }
            else if (attr == R.styleable.StateViewLayout_noNetIcon){
                noNetIcon = array.getResourceId(attr,0);
            }
            else if (attr == R.styleable.StateViewLayout_noNetText){
                noNetText = array.getString(attr);
            }
            else if (attr == R.styleable.StateViewLayout_current_state){
                CURRENT_STATE = array.getInt(attr,0);
            }
            else if (attr == R.styleable.StateViewLayout_showTextColor){
                showTextColor = array.getColor(attr, Color.BLACK);
            }
            else if (attr == R.styleable.StateViewLayout_loadViewSize){
                loadViewSize = array.getDimension(attr, 100);
            }
            else if (attr == R.styleable.StateViewLayout_NoNetLayoutResID){
                NoNetLayoutResID = array.getResourceId(attr, 0);
            }
            else if (attr == R.styleable.StateViewLayout_NoDataLayoutResID){
                NoDataLayoutResID = array.getResourceId(attr, 0);
            }
            else if (attr == R.styleable.StateViewLayout_LoadingLayoutResID){
                LoadingLayoutResID = array.getResourceId(attr, 0);
            }
        }
        array.recycle();

        setMode(CURRENT_STATE);
    }


    /**
     * @param state
     */
    public void setMode(int state){
        removeAllViews();
        CURRENT_STATE = state ;
        initView() ;
        addChildView();
    }

    private View NoNetView;
    private View NoDataView;
    private View LoadView;


    private void initView() {
        if (NoNetLayoutResID!=0){
            addView(NoNetView = LayoutInflater.from(getContext()).inflate(NoNetLayoutResID,this,false));
        }else {
            addNoNetView();
        }

        if (NoDataLayoutResID!=0){
            addView(NoDataView = LayoutInflater.from(getContext()).inflate(NoDataLayoutResID,this,false));
        }else {
            addNoDataView();
        }

        if (LoadingLayoutResID!=0){
            addView(LoadView = LayoutInflater.from(getContext()).inflate(LoadingLayoutResID,this,false));
        }else {
            addLoadingView();
        }
        none();
    }



    private void addChildView(){
        setVisibility(VISIBLE);
        switch (CURRENT_STATE){
            case LOADING:
                if (loadingView!=null){
                    loadingView.setVisibility(VISIBLE);
                }
                if (noDataCommonStateView!=null){
                    noDataCommonStateView.setVisibility(GONE);
                }
                if (noNetCommonStateView!=null){
                    noNetCommonStateView.setVisibility(GONE);
                }


                if (LoadView!=null){
                    LoadView.setVisibility(VISIBLE);
                }
                if (NoNetView!=null){
                    NoNetView.setVisibility(GONE);
                }
                if (NoDataView!=null){
                    NoDataView.setVisibility(GONE);
                }


                break;
            case NO_DATA:
                if (loadingView!=null){
                    loadingView.setVisibility(GONE);
                }
                if (noDataCommonStateView!=null){
                    noDataCommonStateView.setVisibility(VISIBLE);
                }
                if (noNetCommonStateView!=null){
                    noNetCommonStateView.setVisibility(GONE);
                }


                if (NoNetView!=null){
                    NoNetView.setVisibility(GONE);
                }
                if (NoDataView!=null){
                    NoDataView.setVisibility(VISIBLE);
                }
                if (LoadView!=null){
                    LoadView.setVisibility(GONE);
                }
                break;
            case NO_NET:
                if (loadingView!=null){
                    loadingView.setVisibility(GONE);
                }
                if (noDataCommonStateView!=null){
                    noDataCommonStateView.setVisibility(GONE);
                }
                if (noNetCommonStateView!=null){
                    noNetCommonStateView.setVisibility(VISIBLE);
                }


                if (LoadView!=null){
                    LoadView.setVisibility(GONE);
                }
                if (NoNetView!=null){
                    NoNetView.setVisibility(VISIBLE);
                }
                if (NoDataView!=null){
                    NoDataView.setVisibility(GONE);
                }
                break;
            case NONE:
                none();
                break;
        }
    }



    /**
     */
    private void none() {
        setVisibility(GONE);
        if (noDataCommonStateView!=null){
            noDataCommonStateView.setVisibility(GONE);
        }

        if (noNetCommonStateView!=null){
            noNetCommonStateView.setVisibility(GONE);
        }

        if (loadingView!=null){
            loadingView.setVisibility(GONE);
        }

        if (NoNetView!=null){
            NoNetView.setVisibility(GONE);
        }

        if (NoDataView!=null){
            NoDataView.setVisibility(GONE);
        }

        if (LoadView!=null){
            LoadView.setVisibility(GONE);
        }
    }


    /**
     * 没有 网络的view
     */
    private void addNoNetView() {
        noNetCommonStateView = new CommonStateView(getContext());
        noNetCommonStateView.setIcon(noNetIcon,noNetText);
        noNetCommonStateView.setColor(showTextColor);
        addView(noNetCommonStateView,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,Gravity.CENTER));
    }


    /**
     * 没有数据
     */
    private void addNoDataView() {
        noDataCommonStateView = new CommonStateView(getContext());
        noDataCommonStateView.setIcon(noDataIcon,noDataText);
        noDataCommonStateView.setColor(showTextColor);
        addView(noDataCommonStateView,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,Gravity.CENTER));
    }


    /**
     * @param t
     */
    public void setText(String t){
        if (noDataCommonStateView!=null){
            noDataCommonStateView.setText(t);
        }
    }

    /**
     * @param t
     */
    public void setText(SpannableString t){
        if (noDataCommonStateView!=null){
            noDataCommonStateView.setText(t);
        }
    }

    /**
     * @param t
     */
    public void setText(SpannableStringBuilder t){
        if (noDataCommonStateView!=null){
            noDataCommonStateView.setText(t);
        }
    }


    /**
     * 加载中
     */
    private void addLoadingView() {
        loadingView = new LoadingView(getContext());
        if (loadViewSize!=0){
            loadingView.setSize(loadViewSize,loadViewSize);
        }
        addView(loadingView,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.CENTER));
    }



    /**
     * @param id
     */
    public void setGif(int id){
        if (loadingView!=null){
            loadingView.setGifIconAnim(id);
        }
    }


    /**
     * 绑定吴网络情况
     * @param adapter
     */
    public void setNoNetAdapter(Adapter adapter) {
        if (NoNetView!=null && adapter!=null){
            adapter.onBindView(NoNetView);
        }
    }

    /**
     * 绑定吴网络情况
     * @param adapter
     */
    public void setNoDataAdapter(Adapter adapter) {
        if (NoDataView!=null && adapter!=null){
            adapter.onBindView(NoDataView);
        }
    }

    /**
     * 绑定吴网络情况
     * @param adapter
     */
    public void setLoadAdapter(Adapter adapter) {
        if (LoadView!=null && adapter!=null){
            adapter.onBindView(LoadView);
        }
    }
}
